package com.anxin.act.listener;

import com.anxin.common.utils.StringUtils;
import com.anxin.common.utils.sys.DeptUtils;
import com.anxin.common.utils.sys.UserUtils;
import com.anxin.sys.system.dao.SysPostDao;
import com.anxin.sys.system.entity.SysDept;
import com.anxin.sys.system.entity.SysPost;
import com.anxin.sys.system.entity.SysUser;
import com.anxin.sys.system.service.ISysPostService;
import org.activiti.engine.delegate.DelegateExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

/**
 * 岗位监听器
 */
@Configuration
public class PostListener {

    @Autowired
    private SysPostDao sysPostDao;
    @Autowired
    private ISysPostService sysPostService;

    /**
     * 例子 ${postListener.judgePost(execution,'总经理,路总')}
     * 判断流程申请人是否是某个岗位
     * @param execution 流程运行对象
     * @param post 岗位，多个逗号分隔
     * @return
     */
    public boolean judgePost(DelegateExecution execution,String post){
        // 申请人ID
        String applyUserName = execution.getVariable("applyUserName", String.class);
        // 获取申请人
        SysUser sysUser = UserUtils.getUserByUserName(applyUserName);
        // 获取用户岗位
        List<String> postIds = sysPostService.selectPostListByUserId(sysUser.getId());
        for (String postId : postIds) {
            SysPost sysPost = sysPostDao.selectPostById(postId);

            String[] posts = post.split(",");
            for (int i = 0; i < posts.length; i++) {
                if (posts[i].equals(sysPost.getPostName())){
                    return true;
                }
            }
        }

        return false;
    }


}
